<?php
namespace app\index\controller;

use think\Db;

use SeasLog;
use databaseClass\database;
use app\index\model\Children;
use app\index\model\ProjectClasses;
use app\index\model\Index as IndexModel;

class Index
{
    public function index()
    {
        /********多表查询******/
        // $a = DB::table('project_classes')
        // ->alias('a')
        // ->join('children b','a.id = b.father_id')
        // ->join('projects c','b.id = c.project_class_id')
        // ->select();
        /***********关联*******/
        $b = new IndexModel();
        $b = $b->change('projects');
        $a = Children::get(2);
        // $a = new ProjectClasses();
        // return $a->test();
        //一对多查询
        // $a = $class->children->toArray();
        $a = $a->comments;
        // return $class;
        //一对多跨表查询
        // $a = $class->projects()->order('project_class_id')->select();
        // return $b;
        return $a;

        /********改字段名******/

        // $a = DB::table('users')->select()->toArray();
        //假设要查询的是全部字段,其中部分字段要修改
        // foreach ($a as $val) {
        //     $val['修改成的字段名'] = $val['被修改的字段名'];
        //     unset( $val['被修改的字段名']);
        // }
        // return $a;

        //如果是要查询部分字段,其中部分字段要设置别名,可以使用 filed 来完成

        /*******返回视图******/
        // return view('', [
        //     'name' => 'world',
        //     'action' => 'hello',
        //     'title' => 'dqw',
        //     'test' => ['a' => 'apple', 'b' => 'base']
        // ]);
        /*******phpinfo******/
        // phpinfo();

        /*******seaslog******/
        // $path = SeasLog::setBasePath(LOG_PATH);

        // $path = SeasLog::getBasePath();
        // $path = SeasLog::getLastLogger();
        // SeasLog::setLogger('index');
        // $path = SeasLog::getLastLogger();
        // SeasLog::debug('555');
        // SeasLog::log('error','666ss');
        // SeasLog::debug('55775');
        // $path = SeasLog::getBuffer();
        // $path = SeasLog::flushBuffer();
        // $path = 1;
        // return $path;
        // dump(SeasLog::analyzerDetail('ERROR','/index/20180930.ERROR'));

        /***********更改数据库*********/

        // 产品图片
        // $data = array();
        // $i = 0;
        // $a = Db::table('projects_copy')->select();
        // foreach ($a as  $value) {
        //     $value['image'] = json_decode($value['image']);
        //    foreach ($value['image']  as $url) {
        //       $data[$i]['image'] = $url;
        //       $data[$i]['father_id'] = $value['id'];
        //       $i++;
        //    } 
        // }
        // $a = Db::name('image')->insertAll($data);
        // return $a;

        //用户爱好和收藏
        // $data = array();
        // $i = 0;
        // $a = Db::table('users')->select();
        // foreach ($a as $value) {
        //     $value['collect'] = json_decode($value['collect'], true);
        //     if (!empty($value['collect'])) {
        //         foreach ($value['collect'] as $key => $id) {
        //             $data[$i]['project_id'] = $id["$" . "oid"];
        //             $data[$i]['father_id'] = $value['id'];
        //             $i++;
        //         }
        //     }
        // }
        // $a = Db::name('collect')->insertAll($data);
        // return $a;

        //分类
        // $data = array();
        // $i = 0;
        // $a = Db::table('projectclasses')->select();
        // foreach ($a as $value) {
        //     $value['children'] = json_decode($value['children'],true);
        //     if (!empty($value['children'])) {
        //         foreach ($value['children'] as $key => $id) {
        //             $data[$i]['id'] = $id["_id"]['$'.'oid'];
        //             $data[$i]['name'] = $id["name"];
        //             $data[$i]['father_id'] = $value['id'];
        //             $i++;
        //         }
        //     }
        // }
        // $a = Db::name('children')->insertAll($data);
        // return $a;

        // 方案表,子类表改id
        // $a = DB::table('children')->select();
        // foreach ($a as $key => $value) {
        //     $data = [
        //     'projectClassId' => $value['id'],
        //     ];
        //     DB::table('projects')->where('projectClassId', $value["_id"])->update($data);
        // }

        //收藏表 点赞表 改 方案id 
        // $a = DB::table('projects')->select();
        //  foreach ($a as $key => $value) {
        //     $data = [
        //     'project_id' => $value['id'],
        //     ];
        //     DB::table('like')->where('project_id', $value["_id"])->update($data);
        // }

        /*******validate******/
        // $data = [
        //     'name' => '55',
        //     'age' => '',
        //     'adress' => '',
        //     'email' => 'kogaleunggmail.com'
        // ];
        // $va = validate('Users');        
        // if (!$va->scene('qwe')->check($data)) {
        //     return dump($va->getError());
        // }
        // else{
        //     return 'OK';
        // }

    }
}
